package com.olmarket.core.dao;

import com.olmarket.core.dto.GoodDetailResult;
import com.olmarket.core.dto.GoodParam;
import com.olmarket.core.dto.RequestDetailResult;
import com.olmarket.core.dto.RequestsParam;
import com.olmarket.core.entity.*;
import org.apache.ibatis.annotations.*;

import java.util.List;


//TODO:适配service(可能要同时修改userMapper.xml,主要是登录&注册、用户信息、商品&需求、商品类型管理相关)

@Mapper
public interface UserDAO {

    /*
        登录&注册相关开始
     */
    @Select("select * from user_student where userid = #{userId}")
    UserStudent getUserStudentByUserId(int userId);

    @Select("select * from user_student where studentId = #{studentId}")
    UserStudent getStudentByStudentId(int studentId);

    @Select("select user_id from user_group where id = #{id}")
    int getUserIdByGroupId(int id);

    @Select("select * from user where username = #{username} and user_type = #{userType}")
    User getUserByNameAndUserType(String username, int userType);

    @Select("select * from user where username = #{username}")
    User getUserByUsername(String username);

    @Update("update user set password = #{newPassword} where id = #{userId}")
    int updateUserPassword(int userId, String newPassword);

    @Insert("insert into user " +
            "(username, password, usertype) " +
            "values " +
            "(#{username},#{password},#{userType})")
    int createUser(User user);

    @Delete("")
    int deleteUser(int id);

    @Delete("")
    int deleteUserBatch(List<Integer> ids);

    /*
        登录&注册相关结束
     */

    /*
        用户相关开始
     */

    //创建用户
    @Insert("insert into user_student " +
            "(userid, name, sex, address, qq, tel) " +
            "values " +
            "(#{userId},#{name},#{sex},#{address},#{qq},#{tel})")
    int createStudent(UserStudent userStudent);

    //修改用户信息
    @Update("")
    int updateStudent(UserStudent userStudent);

    //删除用户
    @Delete("")
    int deleteStudent(int studentId);

    //查询用户
    List<UserStudent> getStudentList(String keyword, int type);

    //批量删除用户
    int deleteStudentBatch(List<Integer> Ids);

    //根据id查询用户信息
    @Select("")
    UserStudent searchStudentById(String id);

    //查找所有用户
    @Select("")
    List<UserStudent> getAllStudentId();
	
   @Update({"update user_student" +
            " set name = #{name}," +
            " sex = #{sex}," +
            " address = #{address}," +
            " qq = #{qq}," +
            " tel = #{tel}" +
            " where studentid = #{studentId};"})
    int modifyStudent(UserStudent userStudent);


    /*
        用户相关结束
     */

    /*
        商品相关开始
     */

    //查询商品信息(可以带关键词和类型)
    List<GoodInfo> getGoodList(String keyword, int typeId);

    //查询商品详细信息(即包括用户信息)
    GoodDetailResult getGoodDetail(int goodId);

    //根据Id查询特定用户的商品
    @Select("select * from goods where studentid = #{studentId}")
    List<GoodInfo> getGoodListByStudentId(int studentId);

    //添加商品
    @Insert("insert into goods" +
            " (goodid,studentid,goodname,typeid,price,description,photo,issold)" +
            " values" +
            " (#{goodId},#{studentId},#{goodName},#{typeId},#{price},#{description},null,#{isSold})")
    int addGood(GoodInfo goodInfo);

    @Update("update goods " +
            "set " +
            "goodname = #{goodName}, " +
            "typeid = #{goodType}, " +
            "price = #{price}, " +
            "description = #{description} " +
            "where goodid = #{goodId} ")
    int modifyGood(GoodParam goodParam);

    //删除商品
    @Delete("delete from goods where goodid = #{goodId}")
    int deleteGood(int goodId);

    //设置商品已售出
    @Update("update goods set issold = 1 where goodid = #{goodId}")
    int setGoodSold(int goodId);

    /*
        商品相关结束
     */

    /*
        需求相关开始
     */

    @Insert("insert into requests " +
            "(studentid, requestname, typeid, description, price, issupplied) " +
            "values " +
            "(#{studentId},#{requestName},#{typeId},#{description},#{price},#{isSupplied})")
    int addRequest(RequestInfo requestInfo);

    //查询需求信息
    List<RequestInfo> getRequestList(String keyword, int typeId);

    //查询商品详细信息(即包括用户信息)
    RequestDetailResult getRequestDetail(int requestId);

    //根据Id查询特定用户的需求
    @Select("select * from requests where studentid = #{studentId}")
    List<RequestInfo> getRequestListById(int studentId);

    //得到所有需求
    @Select("select * from requests where issupplied = 0")
    List<RequestInfo> getAllRequests();

    //得到所有商品
    @Select("select * from goods where issold = 0")
    List<GoodInfo> getAllGoods();

    //删除需求
    @Delete("delete from requests where requestid = #{requsetId}")
    int deleteRequest(int requestId);

    //修改需求
    @Update("update requests " +
            "set " +
            "requestname = #{requestName}, " +
            "typeid = #{requestType}, " +
            "description = #{description}, " +
            "price = #{price} " +
            "where requestid = #{requestId}")
    int modifyRequest(RequestsParam requestsParam);

    //设置需求满足
    @Update("update requests " +
            "set " +
            "issupplied = 1 " +
            "where requestid = #{requestId}")
    int setRequestSupplied(int requestId);

    /*
        需求相关结束
     */

    /*
        商品类型相关开始
     */

    //添加类型
    @Insert("insert into type (typename) values (#{typeName})")
    int addType(Type type);

    //删除类型
    @Delete("delete from type where typeid = #{typeId}")
    int deleteType(int typeId);

    //修改类型
    @Update("update type set typename = #{typeName} where typeid = #{typeId}")
    int updateType(Type type);

    @Select("Select * from type")
    List<Type> getAllTypes();

    /*
        商品类型相关结束
     */

    /**
     * 在这里写了两种新建用户的方式(具体查看sql语句)：
     * 1、id自增：即id每次加1这种
     * 2、利用UUID()生成id，在存入用户
     * 若采用第1种方式，需要对数据库中的id字段做一些修改，将id的类型改为int型，同时定义为可以自增。
     * 若采用第2种方式，则将将id的类型改为varchar型，同时取消自增。
     * <p>
     * 无论再用那种方式，都需要注意实体类中的类型是否与数据库一致，若不一致，项目运行时报错。
     */
}
